package com.swsc.solarverse.common.util;

import com.swsc.solarverse.common.Constant;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Author anonymous
 * @Date 2022/6/13 16:42
 * @Version 1.0
 * @Description CodeUtils
 */
public class CodeUtils {
    private static final String FORMAT_ORGAN_NO = "000000000";
    /**
     * Q: 第几期
     * F: 方阵
     * X: 箱变
     * N: 逆变器
     * H: 汇流箱
     * Z: 组串
     */
    private final static String CODE_TEMPLATE = "QFXNHZ";

    public static String genDictCode(String dictType, String maxCode){
        maxCode = maxCode.replace(dictType, "");
        return dictType + autoGenericCode(maxCode, 3);
    }

    /**
     * 不够位数的在前面补0，
     *
     * @param code
     * @param num  保留num的长度位数字
     * @return
     */
    private static String autoGenericCode(String code, int num) {
        String result = "";
        // 保留num的位数
        // 0 代表前面补充0
        // num 代表长度为4
        // d 代表参数为正数型
        result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        return result;
    }

    /**
     * 从设备编码中提取组串编号
     * @param deviceCode
     * @return
     */
    public static Long extractStringNum(String deviceCode){
        if(StringUtils.isNotBlank(deviceCode)){
            String stringNumStr = extractStringNumStr(deviceCode);
            if(StringUtils.isNotBlank(stringNumStr)){
                stringNumStr = stringNumStr.replace("，", ",");
                stringNumStr = stringNumStr.replace("、", ",");
                if(stringNumStr.contains(",")){
                    stringNumStr = stringNumStr.split(",")[0];
                }
            }
            if(RegexUtils.isNumeric(stringNumStr)){
                return Long.parseLong(stringNumStr);
            }
        }

        return null;
    }

    public static String extractStringNumStr(String deviceCode){
        if(StringUtils.isNotBlank(deviceCode)){
            // 设备编码用中划线-且体现出方阵/分区、汇流箱、组串
            String[] modelSplitStrs = deviceCode.split("-");
            // 最后一段字符为组串
            return modelSplitStrs[modelSplitStrs.length - 1];
        }
        return null;
    }


    /**
     * 租户编号生成
     * @param maxTenantNo
     * @return
     */
    public static Integer genTenantNo(Integer maxTenantNo){
        if(null == maxTenantNo){
            return Constant.DEFAULT_TENANT_NO + Constant.DEFAULT_TENANT_STEP;
        }else {
            return maxTenantNo + Constant.DEFAULT_TENANT_STEP;
        }
    }

    /**
     * 批次号(15位, 编号规则: 上传日期或巡检日期8位+电站编号3位(不足用0在前面补齐)+ 类型2位(默认01) + 序号2位(同一电站一天内巡检的第几次, 不足用0在前面补齐))
     * @param organNo
     * @param patrolTime
     * @param num
     * @return
     */
    public static String generateBatchNo(int organNo, LocalDateTime patrolTime, int num){
        // 20230328 100302001 01 01
        if(null == patrolTime){
            patrolTime = LocalDateTime.now();
        }
//        String dateStr = LocalDateTimeUtils.time2Str(patrolTime, "yyyyMMdd");
//        String organNoStr = String.format("%9d", organNo);
        return LocalDateTimeUtils.time2Str(patrolTime, "yyMMdd").concat(String.format("%09d", organNo)).concat("01").concat(String.format("%02d", num));
    }

    public static String generateTaskNo(int organNo, LocalDateTime patrolTime, int num){
        // 20230328 100302001 01 01
        if(null == patrolTime){
            patrolTime = LocalDateTime.now();
        }
        return generateTaskNoPrefix(organNo, patrolTime).concat(String.format("%02d", num));
    }

    public static String generateTaskNoPrefix(int organNo, LocalDateTime patrolTime){
        // 20230328 100302001 01 01
        return generateTaskNoPrefix(organNo, patrolTime, "01");
    }

    public static String generateTaskNoPrefix(int organNo, LocalDateTime patrolTime, String type){
        // 20230328 100302001 01 01
        if(null == patrolTime){
            patrolTime = LocalDateTime.now();
        }
        if(null == type || "".equals(type.trim())) {
            type = "01";
        }
        return LocalDateTimeUtils.time2Str(patrolTime, "yyMMdd").concat(String.format("%09d", organNo)).concat(type);
    }

    public static String generatePatrolNo(String taskNo, int num){
        return taskNo.concat(String.format("%02d", num));
    }

    public static String generateTimeNo(int organNo, LocalDateTime dateTime){
        if(null == dateTime){
            dateTime = LocalDateTime.now();
        }
        String timeStr = LocalDateTimeUtils.time2Str(dateTime, "yyMMddHHmmssSSS");
        return String.format("%09d", organNo).concat(timeStr);

    }
    public java.util.Set<String> parseRawCode(String rawCode){
        if(null != rawCode && !"".equals(rawCode.trim())){
            if(rawCode.contains(",")){
                String[] codeArr = rawCode.split(",");
            }

        }
        return Collections.EMPTY_SET;
    }

    public static String genTurbineCode(Integer organNo, String turbineName){
        DecimalFormat df = new DecimalFormat(FORMAT_ORGAN_NO);
        String organNoStr = df.format(organNo);
        String suffix = turbineName.substring(turbineName.indexOf("号风机"));
//        String turbineNo = turbineName.replaceAll("号风机", "");
        String turbineNo = turbineName.replaceAll(suffix, "");
        turbineNo = RegexUtils.removeChinese(turbineNo);
        turbineNo = turbineNo.replace("B", "");
        turbineNo = turbineNo.replace("Q", "");
        turbineNo = turbineNo.replace("_", "");
        turbineNo = turbineNo.replace("-", "");
        String turbineNoStr = String.format("%04d", Integer.parseInt(turbineNo));
        return organNoStr + "61" + turbineNoStr;
    }
    public static String genEcpylonCode(Integer organNo, String eclylonName){
        DecimalFormat df = new DecimalFormat(FORMAT_ORGAN_NO);
        String organNoStr = df.format(organNo);
        String suffix = eclylonName.replace("号电塔", "");
        suffix = suffix.replace("电塔", "");
        suffix = suffix.replace("号塔", "");
        String turbineNoStr = String.format("%04d", Integer.parseInt(suffix));
        return organNoStr + "71" + turbineNoStr;
    }
    public static String getCode(String vectorDeviceCode, Byte stringIndex) {
        if (StringUtils.isNotBlank(vectorDeviceCode)) {
            String deviceCodePrefix = vectorDeviceCode.substring(0, 12);
            if (vectorDeviceCode.contains(",")) {
                String[] splitCodeArr = vectorDeviceCode.split(",");
                if (null != splitCodeArr) {
                    if (splitCodeArr.length == 1) {
                        return splitCodeArr[0];
                    } else {
                        if (splitCodeArr[0].length() == splitCodeArr[1].length()) {
                            if(splitCodeArr[0].length() > 12){
                                String deviceCodePrefix1 = splitCodeArr[0].substring(0, 12);
                                String idx = splitCodeArr[0].replace(deviceCodePrefix1.concat("-"), "");
                                if(Objects.equals(Byte.parseByte(idx), stringIndex)){
                                    return splitCodeArr[0];
                                }
                            }
                            if(splitCodeArr[1].length() > 12){
                                String deviceCodePrefix1 = splitCodeArr[1].substring(0, 12);
                                String idx = splitCodeArr[1].replace(deviceCodePrefix1.concat("-"), "");
                                if(Objects.equals(Byte.parseByte(idx), stringIndex)){
                                    return splitCodeArr[1];
                                }
                            }
                            return deviceCodePrefix.concat("-").concat(String.format("%02d", stringIndex));
                        }else {
                            if(splitCodeArr[0].length() > 12){
                                String deviceCodePrefix1 = splitCodeArr[0].substring(0, 12);
                                String idx = splitCodeArr[0].replace(deviceCodePrefix1.concat("-"), "");
                                if(Objects.equals(Byte.parseByte(idx), stringIndex)){
                                    return splitCodeArr[0];
                                }
                            }
                            return deviceCodePrefix.concat("-").concat(String.format("%02d", stringIndex));
                        }
//                        if (RegexUtils.isStandardStringCode(splitCodeArr[0]) && splitCodeArr[1].length() == 2) {
//                            return Arrays.asList(splitCodeArr[0], vectorDeviceCode.substring(0, 12).concat("-").concat(splitCodeArr[1]));
//                        }
                    }
                }
            } else {
                return vectorDeviceCode;
            }
        }
        return null;
    }

    public static List<String> parseCode(String vectorDeviceCode, Byte stringIndex) {
        if (StringUtils.isNotBlank(vectorDeviceCode)) {
            if (null != stringIndex) {
                String deviceCodePrefix = vectorDeviceCode.substring(0, 12);
                return Arrays.asList(deviceCodePrefix.concat("-").concat(String.format("%02d", stringIndex)));
            } else {
                if (vectorDeviceCode.contains(",")) {
                    String[] splitCodeArr = vectorDeviceCode.split(",");
                    if (null != splitCodeArr) {
                        if (splitCodeArr.length == 1) {
                            return Arrays.asList(splitCodeArr[0]);
                        } else {
                            if (splitCodeArr[0].length() == splitCodeArr[1].length()) {
                                return Arrays.asList(splitCodeArr[0], splitCodeArr[1]);
                            }
                            if (RegexUtils.isStandardStringCode(splitCodeArr[0]) && splitCodeArr[1].length() == 2) {
                                return Arrays.asList(splitCodeArr[0], vectorDeviceCode.substring(0, 12).concat("-").concat(splitCodeArr[1]));
                            }
                        }
                    }
                } else {
                    return Arrays.asList(vectorDeviceCode);
                }

            }
        }
        return null;
    }

    public static String generateProjectSTaskNo(int organNo, LocalDateTime patrolTime, int num){
        // 20230328 100302001 01 01
        if(null == patrolTime){
            patrolTime = LocalDateTime.now();
        }
        return generateTaskNoPrefix(organNo, patrolTime, "41").concat(String.format("%02d", num));
    }
    public static String generateProjectPTaskNo(int organNo, LocalDateTime patrolTime, int num){
        // 20230328 100302001 01 01
        if(null == patrolTime){
            patrolTime = LocalDateTime.now();
        }
        return generateTaskNoPrefix(organNo, patrolTime, "42").concat(String.format("%02d", num));
    }


    private CodeUtils(){}
}
